<template>
  <div v-loading="fullscreenLoading" class="my-iframe-box">
    <iframe v-for="(item, index) in iframeLink" v-show="$route.fullPath === item.fullPath" :key="index" :src="item.fullPath" class="iframe" />
  </div>
</template>

<script>
export default {
  name: 'AvueIframe',
  // eslint-disable-next-line vue/require-prop-types
  data() {
    return {
      fullscreenLoading: false
    }
  },
  computed: {
    iframeLink() {
      return this.$store.state.tagsView.iframeLink
    }
  },
  mounted() {
    this.resize()
    this.load()
  },
  methods: {
    // 显示等待框
    show() {
      this.fullscreenLoading = true
    },
    // 隐藏等待框
    hide() {
      this.fullscreenLoading = false
    },
    // 加载浏览器窗口变化自适应
    resize() {
      window.onresize = () => {
        this.iframeInit()
      }
    },
    // 加载组件
    load() {
      this.hide()
      if (this.$route.path.includes('/myiframe')) {
        if (this.$route.query.src) {
          var flag = true // URL是否包含问号
          if (this.$route.query.src.indexOf('?') === -1) {
            flag = false
          }
          var list = []
          for (var key in this.$route.query) {
            if (key !== 'src' && key !== 'name') {
              if (!this.$route.query.src.includes('menuCode')) {
                list.push(`${key}=${this.$route.query[key]}`)
              }
            }
          }
          list = list.join('&').toString()
          if (flag && !this.$route.query.src.includes('menuCode')) {
            this.iframeUrl = `${this.$route.query.src}${
              list.length > 0 ? `&${list}` : ``
            }`
          }
          if (!flag && !this.$route.query.src.includes('menuCode')) {
            this.iframeUrl = `${this.$route.query.src}${
              list.length > 0 ? `?${list}` : ``
            }`
          }
        }
        // 超时5s自动隐藏等待框，加强用户体验
        let time = 5
        const timeFunc = setInterval(() => {
          time--
          if (time === 0) {
            this.hide()
            clearInterval(timeFunc)
          }
        }, 1000)
        this.iframeInit()
      }
    },
    // iframe窗口初始化
    iframeInit() {
      const iframe = this.$refs.iframe
      // const clientHeight = document.documentElement.clientHeight - (screen > 1 ? 200 : 130)
      const clientHeight = window.innerHeight - 107
      iframe.style.height = `${clientHeight}px`
      if (iframe.attachEvent) {
        iframe.attachEvent('onload', () => {
          this.hide()
        })
      } else {
        iframe.onload = () => {
          this.hide()
        }
      }
    }
  }
}
</script>

<style lang="scss">
.iframe {
  width: 100%;
  height: 100%;
  border: 0;
  overflow: hidden;
  box-sizing: border-box;

  &::-webkit-scrollbar-track-piece{
    background-color: transparent;
    border: 0;
  }
  &::-webkit-scrollbar {
    width: 8px;
    height: 8px;
  }
  &::-webkit-scrollbar-thumb {
    border-radius: 1em;
    background-color: rgba(50,50,50,.1);
  }
}
</style>
